//
//  FaceView.m
//  BaChat
//
//  Created by 刘洋 on 16/6/27.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FaceView.h"
#import "NXFlowLayout.h"
#import "ExpressionCollectionViewCell.h"

@interface FaceView ()<UICollectionViewDataSource,UICollectionViewDelegate,NXFlowLayoutDelegate>


@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)UIPageControl *pageC;



@end


@implementation FaceView

-(void)dealloc{
    Block_release(_faceStringBlock);
    [_collectionView release];
    [_array release];
    [_pageC release];
    [super dealloc];
}





-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubviews];
        
    }
    return self;
    
}


-(void)createSubviews{
     self.array = [NSMutableArray array];
    
    NXFlowLayout *flowLayout = [[NXFlowLayout alloc] init];
    flowLayout.ytDelegate = self;
    flowLayout.edgeInsets = UIEdgeInsetsMake(5, 5, 0, 5);
    flowLayout.colum = 8; //列数
    flowLayout.row = 4;
    flowLayout.rowSpace = 5;
    flowLayout.columSpace = 5;
    
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ROOTWIDTH, ROOTHEIGHT / 3) collectionViewLayout:flowLayout];
    
    
    
    [self.collectionView registerClass:[ExpressionCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    //按页滚动
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self addSubview:self.collectionView];
    [_collectionView release];
    [flowLayout release];
    
    //    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
    //    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    
    for (int i = 1; i < 106; i++) {
        
        if (i < 10) {
            NSString *imageString = [NSString stringWithFormat:@"00%d",i];
                   [self.array addObject:imageString];
        }else if (i > 9 && i < 100){
             NSString *imageString = [NSString stringWithFormat:@"0%d",i];
                   [self.array addObject:imageString];
        }else{
             NSString *imageString = [NSString stringWithFormat:@"%d",i];
                   [self.array addObject:imageString];
        }
        
  
    }
    
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(ROOTWIDTH / 4  , ROOTHEIGHT  / 3 - 40, ROOTWIDTH /2, 30)];
    self.pageC.currentPageIndicatorTintColor = [UIColor grayColor];
    self.pageC.numberOfPages = 4;
    self.pageC.pageIndicatorTintColor = [UIColor blackColor];
    [self addSubview:self.pageC];
    [_pageC release];
    
    
}

-(CGFloat)NXFlowLayout:(NXFlowLayout *)flowLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width{
    
    return ((ROOTHEIGHT / 4)  - 40) / 3;
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%lu",(unsigned long)self.array.count);
    return self.array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ExpressionCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    item.imageString = self.array[indexPath.row];

    return item;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"EmoticonsList" ofType:@"plist"];
    
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:filePath];
    
    NSDictionary *dic =  [arr objectAtIndex:indexPath.row];
    
    NSString *str = [dic objectForKey:@"cht"];
    
    self.faceStringBlock(str);
    
    NSLog(@"%@",str);
}

//UIPageControl 点滚动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageC.currentPage = page;
}
    
    



@end
